// pages/device/list/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
formatTime = require("../../../component/common.js");
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    estates:[],
    estate_names:[],
    roomList:[],
    pageSize:10,
    pageIndex: 1,
    netWork:false,
    loading:false,
    hasData:false,
    isLoading:true,
    estate_id:0,
    showCalendar:false,
    begin_date:'',
    end_date:'',
    remark:'',
    searchValue:'',
    check_time:'',
    is_set_time:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.onHttpEstateList()
      
    this.onHttpInit()
  },
  changeEstate(e){
    console.log('changeEstate',e);
    var estate_id = e.detail;
    this.data.estate_id = estate_id;
    this.refreshdeviceList()
  },
  onChange(val){
    console.log('onChange',val)
    this.setData({
      searchValue: val.detail
    });
  },
  onSearch(val){
    console.log('onSearch',val);
    this.data.searchValue = val.detail;
    this.refreshdeviceList()
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.refreshdeviceList()
  },
  goAddTemp(e){
    var roomId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: './add/index?room_id=' + roomId+'&type=' + type,
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpEstateList: function () {
    var that = this
    fetchApi.httpPost('/estate/admin/list',{
    }, function (res) {
      console.log("获取我管辖的小区列表", res)
      if(res.data.totalcount > 0){
        var estates = res.data.data.estates;
        for (var index in estates) {
          that.data.estates.splice(that.data.estates.length, 0, estates[index]);
        }
      }
      that.data.estate_id = that.data.estates.length > 0 ?  that.data.estates[0].id : 0;
      that.setData({ 
        estates: that.data.estates,
        estate_names:res.data.data.estate_names,
        estate_id:that.data.estates.length > 0 ?  that.data.estates[0].id : 0,
      });
      
      that.onHttpInit()
      // complete
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/estate/check/room/list', {
      keyWord:that.data.searchValue,
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize,
      estate_id:that.data.estate_id,
    }, function (res) {
      console.log("获取房屋信息", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          var rooms = res.data.data.rooms;
          that.data.check_time = res.data.data.check_time;
          wx.setNavigationBarTitle({
            title: that.data.check_time + '抄表',
          })
          that.setData({
            is_set_time: res.data.data.is_set_time,
          })
          if(rooms.length > 0){
            console.log('rooms',rooms)
            for (var index in rooms) {
              that.data.roomList.splice(that.data.roomList.length, 0, rooms[index]);
            }
            that.setData({
              hasData: true,
              roomList: that.data.roomList
            })
          }
        }
      }else{
        Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshdeviceList()
  },
  refreshdeviceList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      roomList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
})